package customer.web.beans;

import java.util.LinkedList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import common.MessageCallback;
import customer.ejb.eaos.InvitationManager;
import customer.ejb.eaos.PersonalizedTPInterface;
import dtos.ExcursionPersTPDTO;
import dtos.InvitationDTO;
import dtos.PersonalizedTPDTO;



@ManagedBean(name="myInvitationsBean")
@ViewScoped
public class MyInvitationsBean {
	
	private DataModel<PersonalizedTPDTO> travels;
	private PersonalizedTPDTO selectedTravel;
	@EJB
	private PersonalizedTPInterface personalizedTPManager;
	private int numberOfPeople;
	@EJB
	private InvitationManager invitationManager;


	public DataModel<PersonalizedTPDTO> getTravels(String username){
		
		List<PersonalizedTPDTO> c = new LinkedList<>();
		for(PersonalizedTPDTO p : personalizedTPManager.getPersonalizedInvitTP(username))
			c.add(p);
		travels = new ListDataModel<>(c);
		return travels;
	}
	
	public void join(String username, PersonalizedTPDTO travel){
		PersonalizedTPDTO t = new PersonalizedTPDTO();
		t.setNomberOfPeople(numberOfPeople);
		t.setConfirmed(true);
		t.setDateOfDeparture(travel.getDateOfDeparture());
		t.setDateOfReturn(travel.getDateOfReturn());
		t.setExcursionPersTps(travel.getExcursionPersTps());
		t.setExcursions(travel.getExcursions());
		t.setHotelBean(travel.getHotelBean());
		t.setMeanOfTransportation1(travel.getMeanOfTransportation1());
		t.setMeanOfTransportation2(travel.getMeanOfTransportation2());
		
		personalizedTPManager.savePersonalizedTP(t, username, new MessageCallback(FacesContext.getCurrentInstance()));
		for (InvitationDTO i : travel.getInvitations()){
			if (i.getInvited().equals(username)){
				invitationManager.removeInvitation(i.getInvitationID());
				travel.getInvitations().remove(i);
			}
		}

		for (ExcursionPersTPDTO e : travel.getExcursionPersTps()){
			personalizedTPManager.saveExcursionPersTP(e);
		}
		
		
		
	}
	
	public void setNumberOfPeople(int n){
		this.numberOfPeople = n;
	}
	
	public int getNumberOfPeople(){
		return this.numberOfPeople;
	}
	
	

}
